package model;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static debug.Debug.DBUG;

public class MapReader {

	private HashMap<Character,Tile> files = new HashMap<Character, Tile>();
	private Tile[][] tiles;
	
//	All tiles are the same
	private void initFiles(){
		files.put('b', new GrassTile());
		files.put('a',new LavaTile());
	}
	
	public MapReader(String map){
		List<List<Tile>> tileArray = new ArrayList<List<Tile>>();
		initFiles();
		File file = new File(map+".txt");
		try {
		    BufferedReader br = new BufferedReader(
		                        new InputStreamReader(
		                        new FileInputStream(file)));
		    String line;
		    int j=0;
		    while((line = br.readLine()) != null){
		    	tileArray.add(new ArrayList<Tile>());
		        for(int i=0;i<line.length();i++){
		        	tileArray.get(j).add(files.get(line.charAt(i)));
		        }
		        j++;
		    }
		    br.close();
		} catch(IOException e) {
		    
		}
		listToArray(tileArray);
	}

	private void listToArray(List<List<Tile>> tileArray ){
		
		tiles = new Tile[tileArray.size()][tileArray.get(0).size()];
		for(int y=0;y<tileArray.size();y++){
			for(int x=0;x<tileArray.get(y).size();x++){
				tiles[y][x]=tileArray.get(x).get(y);
			}
		}
	}
	
	public Tile[][] getTiles(){
		return tiles;
	}
	
}
